package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.dao.RoleDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.Role;
import com.embhealth.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Map<String, Object>> findAllRoleName() {
        List<Map<String,Object>> nameList=roleDao.findAllRole4AddUser();
        return nameList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        long total = page.getTotal();
        List<Role> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void addRole(Role role, Integer[] permissions, Integer[] urls){
        //先添加用户
        roleDao.addRole(role);
        Integer rid = role.getId();
        //依次添加关联关系
        //添加执行权限
        if (permissions!=null) {
            for (Integer permission : permissions) {
                roleDao.addPermission4Role(permission, rid);
            }
        }
        //添加菜单权限
        if (urls!=null) {
            for (Integer url : urls) {
                roleDao.addMenu4Role(url, rid);
            }
        }
    }
    @Override
    public Role findById(Integer id){
        return roleDao.findById(id);
    }
    @Override
    public Integer[] findPermissionsByRoleId(Integer id){
        return roleDao.findPermissionsByRoleId(id);
    }
    @Override
    public Integer[] findUrlsByRoleId(Integer id){
        return roleDao.findUrlsByRoleId(id);
    }
    @Override
    public void edit(Integer[] urls,Integer[] permissions,Role role){
        //修改用户数据
        roleDao.edit(role);
        Integer id = role.getId();
        //删除所有关联数据
        roleDao.deletePermissionByRoleId(id);
        roleDao.deleteMenuByRoleId(id);
        //依次添加
        //添加执行权限
        if (permissions!=null) {
            for (Integer permission : permissions) {
                roleDao.addPermission4Role(permission, id);
            }
        }
        //添加菜单权限
        if (urls!=null) {
            for (Integer url : urls) {
                roleDao.addMenu4Role(url, id);
            }
        }
    }
    @Override
    public void delete(Integer id){
        //先检查是否被关联
        Integer userByRoleId = roleDao.findUserByRoleId(id);
        if (userByRoleId!=null){
            throw new RuntimeException("角色已被关联");
        }else {
            //依次删除角色信息和关联
            roleDao.deleteRole(id);
            roleDao.deletePermissionByRoleId(id);
            roleDao.deleteMenuByRoleId(id);
        }
    }
}
